package com.tju320.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tju320.elm.ElmAdminEntry;
import com.tju320.elm.dao.AdminDao;
import com.tju320.elm.po.Admin;
import com.tju320.elm.util.DBUtil;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Admin getAdminByNameByPass(String adminName, String password) {
		Admin admin = new Admin();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			// 3.准备发送SQL
			String sql = "select * from admin where adminName=? and password=?";
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, adminName);
			pstm.setString(2, password);
			// 执行sql
			rs = pstm.executeQuery();
			if (rs.next()) {
				admin.setAdminId(rs.getInt("adminId"));
				admin.setAdminName(adminName);
				admin.setPassword(password);
			} else {
				admin = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstm, conn);
		}
		return admin;
	}

	public static void main(String[] args) {
		System.out.println(new AdminDaoImpl().getAdminByNameByPass("王磊", "123").getAdminId());
	}

}
